package com.codeczx.mysimpletagimageview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;

/**
 * Created by codeczx on 2017/4/28.
 */

public class TagImageView extends android.support.v7.widget.AppCompatImageView {

    private static final float SQRT2 = (float) Math.sqrt(2);
    private Point point1;
    private Point point2;
    private Point point3;
    private Point point4;
    private float tagWidth = 120;
    private int cornerDistance = 120;
    private Paint textPaint;
    private String tagText = "tagText";
    private Rect mTextBound;
    float textLeft;
    float mTextBottom;

    private Paint paint;

    public TagImageView(Context context) {
        this(context, null);
    }

    public TagImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TagImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setColor(0x9F27CDC0);
        paint.setStrokeWidth(1);
        textPaint = new Paint();
        textPaint.setTextSize(70);
        textPaint.setColor(Color.BLACK);
        mTextBound = new Rect();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Path path3 = new Path();
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        point1 = new Point();
        point2 = new Point();
        point3 = new Point();
        point4 = new Point();
        point1.x = (int) (width - tagWidth*SQRT2 - cornerDistance);
        point1.y = 0;
        point2.x = width - cornerDistance;
        point2.y = 0;
        point3.x = width;
        point3.y = cornerDistance;
        point4.x = width;
        point4.y = (int) (cornerDistance + tagWidth*SQRT2);
        path3.moveTo(point1.x, point1.y);
        path3.lineTo(point2.x, point2.y);
        path3.lineTo(point3.x, point3.y);
        path3.lineTo(point4.x, point4.y);
        path3.close();
        canvas.drawPath(path3, paint);

        textPaint.getTextBounds(tagText, 0, tagText.length(), mTextBound);
        textLeft = width - cornerDistance +(cornerDistance*SQRT2-mTextBound.width())/2;
        mTextBottom = (tagWidth-mTextBound.height())/2+mTextBound.height();
        canvas.rotate(45,width-cornerDistance,0);
        canvas.drawText(tagText, textLeft, mTextBottom, textPaint);

        Log.i("codeczx", "tagWidth:" + tagWidth);
        Log.i("codeczx", "mTexBound.height:" + mTextBound.height());
    }

    private class Point {
        public int x;
        public int y;
    }
}
